package com.ruibang.glass.produce.service.impl;

import com.ruibang.glass.produce.domain.req.ProduceOperateReq;
import com.ruibang.glass.produce.domain.resp.ProduceOperateResp;
import com.ruibang.glass.produce.entity.OperateQuality;
import com.ruibang.glass.produce.entity.ProduceCost;
import com.ruibang.glass.produce.entity.SaleStatistics;
import com.ruibang.glass.produce.mapper.ProduceOperateMapper;
import com.ruibang.glass.produce.service.ProduceOperateService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @program: glass
 * @description: 生产经营服务类
 * @author: wujie
 * @create: 2024-04-23 16:06
 */
@Slf4j
@Service
public class ProduceOperateServiceImpl implements ProduceOperateService {

    @Autowired
    private ProduceOperateMapper operateMapper;

    @Override
    public ProduceOperateResp getProduceOperate(ProduceOperateReq operateReq) {
        ProduceOperateResp resp = new ProduceOperateResp();
        if(operateReq!=null&&operateReq.getRecordDate()!=null){
            String recordDate = operateReq.getRecordDate().format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT));
            OperateQuality operateQuality = operateMapper.queryOperateQuality(recordDate);
            resp.setOperateQuality(operateQuality);

            ProduceCost produceCost = operateMapper.queryProduceCost(recordDate);
            resp.setProduceCost(produceCost);

            SaleStatistics saleStatistics = operateMapper.querySaleStatistics(recordDate);
            resp.setSaleStatistics(saleStatistics);
        }

        return resp;
    }

    @Override
    public void addProduceCost(ProduceCost produceCost) {
        produceCost.setRecordDate(LocalDate.now());
        operateMapper.insert(produceCost);
    }
    @Override
    public void updateProduceCost(ProduceCost produceCost) {
        if(StringUtils.isBlank(produceCost.getCostId())){
            produceCost.setRecordDate(LocalDate.now());
            operateMapper.insert(produceCost);
        }else{
            operateMapper.updateById(produceCost);
        }
    }

}
